/* AboutActivity.java
 *
 * Copyright 2011 Kirill Ashikhmin
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 * 
 */
package org.jimm.app;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jimm.util.Util;

import android.app.Activity;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class AboutActivity extends Activity {

    public static final String LOG_TAG = "Jimm:AboutActivity";
    public static String lang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);

        String firmware = android.os.Build.VERSION.RELEASE;
        String model = android.os.Build.MODEL;
        String id = android.os.Build.ID;
        int code = android.os.Build.VERSION.SDK_INT;
        String platformName = model + "/" + firmware + "/" + id + "/" + code;

        final TextView device = (TextView) findViewById(R.id.device);
        final TextView version = (TextView) findViewById(R.id.version);

        String versionsText = getText(R.string.version) + " " + getVersionName() + "  " + getText(R.string.build) + " " + getVersionCode() + ")";

        device.setText(platformName);
        version.setText(versionsText);

        Handler versRequest = new Handler();
        versRequest.post(new Runnable() {
            public void run() {
                final TextView version = (TextView) findViewById(R.id.version);
                String versionsText = version.getText().toString().trim();
                ;
                final String url = "http://jimm.boomik.ru/version.txt";
                String result = getRequest(url);

                if (result == "Failure!") {
                    result = "1@Error!";
                }
                ;
                String[] newVers = Util.explode(result, '@');
                if (newVers[0] == "Failure!") {
                    newVers[0] = "1";
                }
                ;
                int newVersInt = Integer.parseInt(newVers[0]);
                if (getVersionCode() < newVersInt) {
                    versionsText += getText(R.string.version_new) + " " + newVers[1] + "  " + getText(R.string.build) + " " + newVers[0] + ")" + getText(R.string.download_new) + newVers[0] + ".apk";
                }
                ;
                version.setText(versionsText);
            }
        });
    }


    public int getVersionCode() {
        int v = 0;
        try {
            v = getPackageManager().getPackageInfo(getApplicationInfo().packageName, 0).versionCode;
        } catch (NameNotFoundException e) {
            // Huh? Really?
        }
        return v;
    }

    public String getVersionName() {
        String v = "";
        try {
            v = getPackageManager().getPackageInfo(getApplicationInfo().packageName, 0).versionName;
        } catch (NameNotFoundException e) {
            // Huh? Really?
        }
        return v;
    }


    public String getRequest(String url) {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response;
        try {
            response = client.execute(request);
        } catch (Exception e) {
            return "Failure!";
        }
        return HttpHelper.request(response);
    }

    public static class HttpHelper {

        public static String request(HttpResponse response) {
            String result = "";
            try {
                InputStream in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder str = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
                in.close();
                result = str.toString();
            } catch (Exception ex) {
                result = "Error";
            }
            return result;
        }
    }
}

